package com.hghivln.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hghivln.blog.entity.BlogTag;
import com.hghivln.blog.entity.Tag;
import com.hghivln.blog.mapper.BlogMapper;
import com.hghivln.blog.mapper.BlogTagMapper;
import com.hghivln.blog.mapper.TagMapper;
import com.hghivln.blog.pojo.vo.HotTagVo;
import com.hghivln.blog.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 标签表 服务实现类
 * </p>
 *
 * @author hghivln
 * @since 2021-09-11
 */
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

    @Autowired
    private BlogTagMapper blogTagMapper;

    /**
     * 根据名称搜索标签
     */
    @Override
    public List<Tag> searchByName(String name) {
        QueryWrapper<Tag> qw = new QueryWrapper<>();
        qw.likeRight("name",name);
        return getBaseMapper().selectList(qw);
    }

    /**
     * 根据博客id查询
     */
    @Override
    public List<Tag> getByBlogId(Long id) {
        QueryWrapper<BlogTag> qw = new QueryWrapper<>();
        qw.eq("blog_id", id);
        List<BlogTag> blogTagList = blogTagMapper.selectList(qw);
        return blogTagList.stream().map(e -> {
            Tag tag = new Tag();
            tag.setName(e.getTagName());
            tag.setId(e.getTagId());
            return tag;
        }).collect(Collectors.toList());
    }

    @Override
    public List<HotTagVo> getHotTag() {
        return getBaseMapper().getHotTag();
    }
}
